package cn.jiangjiesheng.bootstrap.integration.datapool;

import com.qpaas.sdk.core.http.QpaasHttpClient;
import com.qpaas.sdk.core.http.QpaasHttpResponse;
import com.qpaas.sdk.dataocean.DatacenterSaveRecordRequest;
import cn.jiangjiesheng.bootstrap.commons.context.UserInfoContext;
import cn.jiangjiesheng.bootstrap.commons.utils.AssertUtils;
import cn.jiangjiesheng.bootstrap.integration.datapool.params.ReqActionParam;
import cn.jiangjiesheng.bootstrap.qpaas.PaasTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WriteService {

    @Autowired
    private PaasTemplate paasTemplate;

    public String write(ReqActionParam.WriteActionParam actionParam) {
        AssertUtils.notNull(actionParam.getEntId(), "企业ID不能为空");
        AssertUtils.notNull(actionParam.getAppId(), "应用ID不能为空");
        AssertUtils.notNull(actionParam.getTableId(), "表ID不能为空");
        QpaasHttpClient client = paasTemplate.getClient();
        DatacenterSaveRecordRequest request = new DatacenterSaveRecordRequest();
        request.setEntId(actionParam.getEntId());
        request.setUserId(UserInfoContext.getCurUserId());
        request.setTableId(actionParam.getTableId());
        request.setData(actionParam.getRecord());
        request.setMethod("info/"+actionParam.getAppId());
        QpaasHttpResponse<Object> response = client.doAction(request);
        boolean success = response.getSuccess();
        if (success) {
            Object result = response.getData();
            System.out.println("返回结果为：" + result);
        } else {
            String message = response.getMessage();
            System.out.println("返回错误结果为：" + message);
        }
        return "";
    }

}
